﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using Api.Core.Common.Extension;
using Api.Core.IServices.User;
using Api.Core.Model.Entity;
using Api.Core.Model;
using Microsoft.AspNetCore.Mvc;
using Api.Core.Model.ViewModel;
using AutoMapper;
using Api.Core.Model.Table;
using Microsoft.AspNetCore.Authorization;

namespace Api.Core.Controllers
{
    public class GroupRoleController : BaseController
    {
         protected readonly IGroupRoleService _groupRoleService;
        protected readonly IMapper _mapper;
        public GroupRoleController(IGroupRoleService groupRoleService,  IMapper mapper)
        {
            _groupRoleService = groupRoleService;
            _mapper = mapper;
        }

        /// <summary>
        /// 新增或修改组权限
        /// </summary>
        /// <param name="model"></param>
        /// <returns></returns>
        [HttpPost("AddGroupRoles")]
        public async Task<BaseResponse<bool>> AddGroupRoles(GroupViewModel model)
        {
            if (model == null || model.GroupId.IsNullOrWhiteSpace())
            {
                return Result(false, Model.StatusCode.ParamsNull, "用户组不能为空");
            }
            return await  _groupRoleService.AddGroupRoles(model);
        }

        /// <summary>
        /// 删除组权限
        /// </summary>
        /// <param name="model"></param>
        /// <returns></returns>
        [HttpPost("DelGroupRole")]
        public async Task<BaseResponse<bool>> DelGroupRole(GroupViewModel model)
        {
            if (model.GroupId.IsNullOrWhiteSpace())
            {
                return Result(false,Model.StatusCode.ParamsNull,"参数GroupId必填");
            }
            var res = await _groupRoleService.DelGroupRole(model);
            return Result(res);
        }


        /// <summary>
        /// 根据GroupId获取组的权限信息
        /// </summary>
        /// <param name="model"></param>
        /// <returns></returns>
        [HttpGet("GetGroupRoles")]
        public async Task<BaseResponse<GroupRoleEntity>> GetGroupRoles(GroupViewModel model)
        {
            var result = await _groupRoleService.GetGroupRoles(model);
            return result;
        }


        /// <summary>
        /// 获取所有角色
        /// </summary>
        /// <returns></returns>
        [HttpGet("GetAllGroupsByCondition")]
        public async Task<BaseResponse<List<GroupTable>>> GetAllGroupsByCondition([FromQuery]GroupEntity model)
        {
            var res = await _groupRoleService.GetAllGroupsByCondition(model);
            return res;
        }
    }
}
